[List表]遍历表

[PB] ListTraverse2.0

简介

遍历全表数据。支持Limit参数控制每次遍历返回的记录数,同时返回响应包中包含TraverseSession字段 和Complete字段,遍历条件:

  • Limit: 控制每次遍历返回的记录条数
  • TraverseSession: 字符串,初始值: 0.0, 用于每次遍历判断遍历位置,游标参数,本次返回的Session值,传入下次遍历Data参数中
  • Complete: bool, 取值范围:False 或 True, 用于判断记录是否遍历完成
  • Select: 指定每次遍历返回的字段
POST http://{Tcaplus_REST_URL}

请求语法

Http请求


#国内idc&dev&其他自建环境:联系dba分配restproxy进程后由dba提供相应Tcaplus_REST_URL
#腾讯云控制台 RESTful Endpoint, ip:80, 端口默认80
http://172.17.0.22
#TcaplusDB本地docker版 RESTful Endpoint, ip:31001, 端口默认31001
http://9.135.8.93:31001

Http头

名称是否必填限制条件说明
x-tcaplus-target是无Tcaplus.ListTraverse
x-tcaplus-version是无Tcaplus3.50.0
x-tcaplus-app-id是无对应业务id编号(aka,集群接入id)
x-tcaplus-zone-id是无对应zone编号(aka,表格组id)
x-tcaplus-protocol-version是无对应protocol版本号,默认2.0
x-tcaplus-table-name是无对应表名
x-tcaplus-pwd-md5是无业务密码(aka,集群访问密码),传入计算后的md5值
x-tcaplus-idl-type是无protobuf
x-tcaplus-result-flag否无0:操作成功后不返回数据1:操作成功后返回和请求一致的数据2:操作成功后返回本次update操作后的数据3:操作成功后返回tcapsvr端操作前的数据
x-tcaplus-data-version-check否1:检测记录版本号,只有当该版本号与服务器端的版本号相同时,该版本号才会自增2:不检测记录版本号,强制把客户端的记录版本号写入到服务器中3:不检测记录版本号,将服务器端的版本号自增
x-tcaplus-data-version否具体的version值

示例:


x-tcaplus-target:Tcaplus.ListTraverse
x-tcaplus-app-id:3
x-tcaplus-zone-id:1
x-tcaplus-protocol-version:2.0
x-tcaplus-table-name:tb_online_list
x-tcaplus-pwd-md5:4e81984efccfb4982333aeb1ff7968d5
x-tcaplus-result-flag:2
x-tcaplus-version:Tcaplus3.50.0
x-tcaplus-data-version-check: 3
x-tcaplus-idl-type:protobuf

Data

使用json格式表示记录相关信息。参数说明:

  • Limit: 必选,限制每次遍历返回的数据条数
  • TraverseSession: 必选, 用于判断遍历循环Session的状态,类似于Offset概念,从响应包的TraverseSession中
  • Select: 可选,指定返回的记录字段
  • Condition:可选,指定条件遍历,如”Condition”: “x > 1”。

说明:早期版本的 restproxy 中该字段拼写为 TraveseSession,现已修正为 TraverseSession。当前版本请求中两种写法都支持,响应中会同时返回 TraverseSession 和 TraveseSession(取值相同),建议统一使用 TraverseSession。

示例:


{
"Limit": 1000,
"TraverseSession": "0.0",
"Select": ["gamesvrid"]
}

完整请求示例

一次性遍历示例

Limit设置比较大,一次就返回所有记录。适合表记录数小的场景


curl -i -XPOST -H 'x-tcaplus-target: Tcaplus.ListTraverse' -H 'x-tcaplus-app-id: 70' -H 'x-tcaplus-zone-id: 1' -H 'x-tcaplus-protocol-version: 2.0' -H 'x-tcaplus-table-name: tb_online_list' -H 'x-tcaplus-pwd-md5: 0972ad76decf4d11a69e2e0d9af335da' -H 'x-tcaplus-result-flag: 1' -H 'x-tcaplus-version: Tcaplus3.50.0' -H 'x-tcaplus-data-version-check: 1' -H 'x-tcaplus-idl-type: protobuf' http://172.17.32.17 -d '{
"Limit": 1,
"TraverseSession": "0.0",
"Select": ["gamesvrid"]
}'

循环遍历示例

Limit设置适中,同时结合TraverseSession和Complete来进行循环遍历判断。以Bash脚本举例:


#!/bin/bash
PROTO_VERSION="2.0"
TCAPLUS_VERSION="Tcaplus3.50.0"
IDL_TYPE="protobuf"
CHECK_POLICY=3
result_flag=2
rest_url="http://172.17.32.17"
app_id=70
zone_id=1
table_name="tb_online_list"
passwd="Test@2020"

#caculate the md5 value of password
md5_passwd=$(echo -n $passwd | md5sum | cut -d ' ' -f1)

function build_headers(){
headers=(
-H "x-tcaplus-target: ${1}"
-H "x-tcaplus-app-id: ${app_id}"
-H "x-tcaplus-zone-id: ${zone_id}"
-H "x-tcaplus-protocol-version: ${PROTO_VERSION}"
-H "x-tcaplus-table-name: ${table_name}"
-H "x-tcaplus-pwd-md5: ${md5_passwd}"
-H "x-tcaplus-result-flag: ${result_flag}"
-H "x-tcaplus-version: ${TCAPLUS_VERSION}"
-H "x-tcaplus-data-version-check: ${CHECK_POLICY}"
-H "x-tcaplus-idl-type: ${IDL_TYPE}"
)
}
function build_traverse_loop_data(){
cat <<EOF
{
"Limit": 1,
"TraverseSession": "$1",
"Select": ["gamesvrid"]
}
EOF
}
function traverse_loop() {
target="Tcaplus.ListTraverse"
build_headers "$target"
complete="False"
limit=1
traverse_session="0.0"
total_num=0
while [ "$complete" == "False" ];do

ret=$(curl -i -XPOST ”${headers[@]}” “$rest_url” -d ”$(build_traverse_loop_data $traverse_session)” | grep -v HTTP | grep -v “json-1.0” | grep -v “content-length” | grep -v ”^$” )

echo “$ret”

#获取complete值,False 或true, False: 表示数据尚未结束,true:表示遍历结束.借助Python3的Json complete=$( echo “$ret” | python3 -c “import sys, json; print(json.load(sys.stdin)[‘Complete’])” )


   
   #获取TraverseSession值,作为下一次遍历定位使用,类似于Offset作用.
traverse_session=$( echo "$ret" | python3 -c "import sys, json; print(json.load(sys.stdin)['TraverseSession'])" )

if [ “$complete” == “False” ];then

  total_num=$(($total_num+1))

fi


done
echo "TotalRecords:$total_num"
}

declare -a headers

#traverse
traverse_loop

返回语法

返回参数说明

参数名说明
ErrorCode返回码
ErrorMsg返回信息
MultiRecordsjson格式,本次遍历返回的记录集合
TotalNum本次遍历返回的记录条数
Completebool类型,False表示数据尚未结束,True表示遍历结束
TraverseSession本次遍历返回的Session游标值,传入下次遍历Data参数中
TraveseSession旧版本字段名,与TraverseSession取值相同,仅为兼容保留

返回示例

判断Complete参数是否为true, 如果为false, 需要通过循环遍历方式继续请求。

一次性返回数据示例

Limit设置比较大的值


{
"ErrorCode": 0,
"ErrorMsg": "Succeed",
"MultiRecords": [{
"RecordVersion": 3,
"Index": 0,
"Record": {
"gamesvrid": "4",
"openid": 4,
"tconndid": 4,
"timekey": "4"
}
}, {
"RecordVersion": 3,
"Index": 1,
"Record": {
"gamesvrid": "2",
"openid": 2,
"tconndid": 2,
"timekey": "2"
}
}, {
"RecordVersion": 3,
"Index": 2,
"Record": {
"gamesvrid": "3",
"openid": 3,
"tconndid": 3,
"timekey": "3"
}
}, {
"RecordVersion": 7,
"Index": 0,
"Record": {
"gamesvrid": "2",
"openid": 1,
"tconndid": 1,
"timekey": "1"
}
}, {
"RecordVersion": 7,
"Index": 1,
"Record": {
"gamesvrid": "11",
"openid": 1,
"tconndid": 1,
"timekey": "1"
}
}, {
"RecordVersion": 7,
"Index": 2,
"Record": {
"gamesvrid": "22",
"openid": 1,
"tconndid": 1,
"timekey": "1"
}
}, {
"RecordVersion": 7,
"Index": 3,
"Record": {
"gamesvrid": "4",
"openid": 1,
"tconndid": 1,
"timekey": "1"
}
}],
"TotalNum": 7,
"Complete": true,
"TraverseSession": "80630.48568512"
}

循环返回数据示例

单次Limit设置比较小(Limit=1), 通过 循环遍历返回的结果,下图所示,2次遍历返回所有记录,总记录条数和上面一次性返回的记录数保持一致,TotalNum都为7。


#Loop-第一次返回结果,TotalNum:3
{
"ErrorCode": 0,
"ErrorMsg": "Succeed",
"MultiRecords": [{
"RecordVersion": 3,
"Index": 0,
"Record": {
"gamesvrid": "4",
"openid": 4,
"tconndid": 4,
"timekey": "4"
}
}, {
"RecordVersion": 3,
"Index": 1,
"Record": {
"gamesvrid": "2",
"openid": 2,
"tconndid": 2,
"timekey": "2"
}
}, {
"RecordVersion": 3,
"Index": 2,
"Record": {
"gamesvrid": "3",
"openid": 3,
"tconndid": 3,
"timekey": "3"
}
}],
"TotalNum": 3,
"Complete": false,
"TraverseSession": "80630.48566720"
}

#Loop-第二次返回结果,TotalNum:4

{
"ErrorCode": 0,
"ErrorMsg": "Succeed",
"MultiRecords": [{
"RecordVersion": 7,
"Index": 0,
"Record": {
"gamesvrid": "2",
"openid": 1,
"tconndid": 1,
"timekey": "1"
}
}, {
"RecordVersion": 7,
"Index": 1,
"Record": {
"gamesvrid": "11",
"openid": 1,
"tconndid": 1,
"timekey": "1"
}
}, {
"RecordVersion": 7,
"Index": 2,
"Record": {
"gamesvrid": "22",
"openid": 1,
"tconndid": 1,
"timekey": "1"
}
}, {
"RecordVersion": 7,
"Index": 3,
"Record": {
"gamesvrid": "4",
"openid": 1,
"tconndid": 1,
"timekey": "1"
}
}],
"TotalNum": 4,
"Complete": false,
"TraverseSession": "80630.48568384"
}

错误码

参考 常见错误码

其它参考文档

[PB List表][C++ SDK]遍历表数据接口说明

[PB List表][Go SDK]遍历表数据接口说明